﻿define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'course/course/index',
                    add_url: 'course/course/add',
                    edit_url: 'course/course/edit',
                    multi_url: 'course/course/multi',
                    del_url: 'course/course/del',
                    table: 'course',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                queryParams: function (params) {
                    params.rel_type = Config.relType;
                    params.rel_id = Config.relId;
                    return params;
                },
                escape: false,
                pk: 'id',
                sortName: 'weigh desc,id desc', //要使排序生效，在前面添加 weigh desc, 并且数据表weigh字段不能全为0,controller里$list=...->order前再添加->order("weigh desc")
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: '课程名称'},
                        {field: 'course_type', title: '课程类型'},
                        {field: 'status', title: '课程状态', formatter: Controller.api.formatter.status},
                        {field: 'is_post', title: '是否发布'},
                        {field: 'class_type', title: '课堂类型'},
                        {field: 'subject_lv', title: '学科层级'},
                        {field: 'school_id', title: '学校'},
                        {field: 'teacher_id', title: '教师岗'},
                        {field: 'manager_id', title: '管理岗'},
                        {field: 'menber_limit', title: '限制人数'},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    title: __('Edit'),
                                    icon: 'fa fa-pencil',
                                    classname: 'btn btn-xs btn-success btn-editone btn-dialog',
                                    url: 'course/course/edit',
                                    extend: 'data-area=["80%","80%"] data-title="编辑课程"'
                                }
                            ]
                        }
                    ]
                ]
            });
            $(document).on('fa.event.success', function(event, data) {
                setTimeout(function() {
                    location.reload();
                }, 1000);
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/add.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
            $(document).on("click","#autocourse",function(){
                $.ajax({
                    url: "/course/course/auto?XDEBUG_SESSION_START=54354",
                    data: {
                        cityId: Config.admin['city_id'],
                        school_id: $("#c-school_id").val(),
                        community_id: $("#c-community_id").val(),
                        addresslv_id:$("#c-address_lv").val(),
                        subject_lv: $("#c-subject_lv").val(),
                        relType:$("#h-rel_type").val(),
                        relId:$("#h-rel_id").val(),
                        hour:$("#c-hour").val(),
                        ckrqsxtj_date_begin:$("#c-ckrqsxtj_date_begin").val(),
                        sksjd:$("#c-sksjd").val(),
                        skipholiday:$("#skip_holiday1").val(),
                        classes:$("#c-classes_id").val(),
                        subjects:$("#c-subject_lv2").val(),
                    },
                    type: "GET",
                    success: function (data2) {
                        data=JSON.parse(data2);
                        $("#resulttable").html(data.data);
                        Toastr.success(data.msg);
                        if(data.count>0){
                            $("#realautocourse").show();
                        }else{
                            $("#realautocourse").hiden();
                        }
                    },
                    error: function (xhr, status, error) {
                    }
                });
            });

            $(document).on("click", "#realautocourse", function () {
                if($("#realautocourse").length>0) $("#realautocourse").hide();
                var postdata = {
                    cityId: Config.admin['city_id'],
                    school_id: $("#c-school_id").val(),
                    community_id: $("#c-community_id").val(),
                    addresslv_id: $("#c-address_lv").val(),
                    subject_lv: $("#c-subject_lv").val(),
                    classes_id: $("#c-classes_id").val(),
                    relType: $("#h-rel_type").val(),
                    relId: $("#h-rel_id").val(),
                    hour: $("#c-hour").val(),
                    ckrqsxtj_date_begin: $("#c-ckrqsxtj_date_begin").val(),
                    sksjd: $("#c-sksjd").val(),
                    skipholiday: $("#skip_holiday1").val(),
                    classes: $("#c-classes_id").val(),
                    subjects: $("#c-subject_lv2").val(),
                };
                $("*[name^='row']").each(function() {
                    var name = $(this).attr('name');
                    var value = $(this).val();
                    postdata[name] = value;
                });
                $.ajax({
                    url: "/course/course/auto?real=1&XDEBUG_SESSION_START=54354",
                    data: postdata,
                    type: "POST",
                    success: function (data2) {
                        data = JSON.parse(data2);
                        Toastr.success(data.msg);
                    },
                    error: function (xhr, status, error) {
                    }
                });
            });
        },
        edit: function () {
            var row = JSON.parse($('#row').val());
            var script = document.createElement('script');
            script.src = '/assets/js/backend/common/edit.js';
            document.head.appendChild(script);
            Controller.api.bindevent();
            $(document).on("click","#autocourse",function(){
                $.ajax({
                    url: "/course/course/auto?XDEBUG_SESSION_START=54354",
                    data: {
                        cityId: Config.admin['city_id'],
                        school_id: $("#c-school_id").val(),
                        community_id: $("#c-community_id").val(),
                        addresslv_id:$("#c-address_lv").val(),
                        subject_lv: $("#c-subject_lv").val(),
                        classes_id: $("#c-classes_id").val(),
                        relType:$("#h-rel_type").val(),
                        relId:$("#h-rel_id").val(),
                        hour:$("#c-hour").val(),
                        ckrqsxtj_date_begin:$("#c-ckrqsxtj_date_begin").val(),
                        sksjd:$("#c-sksjd").val(),
                        skipholiday:$("#skip_holiday1").val(),
                        classes: $("#c-classes_id").val(),
                        subjects:$("#c-subject_lv2").val(),
                    },
                    type: "GET",
                    success: function (data2) {
                        data=JSON.parse(data2);
                        $("#resulttable").html(data.data);
                        Toastr.success(data.msg);
                        if(data.count>0){
                            $("#realautocourse").show();
                        }else{
                            $("#realautocourse").hiden();
                        }
                    },
                    error: function (xhr, status, error) {
                    }
                });
            });

            $(document).on("click", "#realautocourse", function () {
                if($("#realautocourse").length>0) $("#realautocourse").hide();
                var postdata = {
                    cityId: Config.admin['city_id'],
                    school_id: $("#c-school_id").val(),
                    community_id: $("#c-community_id").val(),
                    addresslv_id: $("#c-address_lv").val(),
                    subject_lv: $("#c-subject_lv").val(),
                    relType: $("#h-rel_type").val(),
                    relId: $("#h-rel_id").val(),
                    hour: $("#c-hour").val(),
                    ckrqsxtj_date_begin: $("#c-ckrqsxtj_date_begin").val(),
                    sksjd: $("#c-sksjd").val(),
                    skipholiday: $("#skip_holiday1").val(),
                    classes: $("#c-classes_id").val(),
                    subjects: $("#c-subject_lv2").val(),
                };
                $("*[name^='row']").each(function() {
                    var name = $(this).attr('name');
                    var value = $(this).val();
                    postdata[name] = value;
                });
                $.ajax({
                    url: "/course/course/auto?real=1&XDEBUG_SESSION_START=54354",
                    data: postdata,
                    type: "POST",
                    success: function (data2) {
                        data = JSON.parse(data2);
                        Toastr.success(data.msg);
                    },
                    error: function (xhr, status, error) {
                    }
                });
            });
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {//渲染的方法
                url: function (value, row, index) {
                    return '<div class="input-group input-group-sm" style="width:250px;"><input type="text" class="form-control input-sm" value="' + value + '"><span class="input-group-btn input-group-sm"><a href="' + value + '" target="_blank" class="btn btn-default btn-sm"><i class="fa fa-link"></i></a></span></div>';
                },
                ip: function (value, row, index) {
                    return '<a class="btn btn-xs btn-ip bg-success"><i class="fa fa-map-marker"></i> ' + value + '</a>';
                },
                status: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="course/course/status" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status == 0 ||row.status == null ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
                changeStatus: function (value, row, index) {
                    //添加上btn-change可以自定义请求的URL进行数据处理
                    return '<a class="btn-change text-success" data-url="course/course/course/course/changeStatus" data-confirm="确认切换状态？" data-id="' + row.id + '"><i class="fa ' + (row.status === 0 ? 'fa-toggle-on fa-flip-horizontal text-gray' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            events: {//绑定事件的方法
                ip: {
                    //格式为：方法名+空格+DOM元素
                    'click .btn-ip': function (e, value, row, index) {
                        e.stopPropagation();
                        var container = $("#table").data("bootstrap.table").$container;
                        var options = $("#table").bootstrapTable('getOptions');
                        //这里我们手动将数据填充到表单然后提交
                        $("form.form-commonsearch [name='ip']", container).val(value);
                        $("form.form-commonsearch", container).trigger('submit');
                        Toastr.info("执行了自定义搜索操作");
                    }
                },
            }
        }
    };
    return Controller;
});